package dubbo.server4.impl.common;

public class StringHelper {
	/*public static void main(String[] args){
		String s="sdfgertdgdfg,";
		String s2=trimEnd(new char[]{','}, s);
	}*/
	/* �ж��ַ����Ƿ�������Ч�Ŀ�ֵ */
	public static boolean isNullOrWhiteSpace(String s) {
		if (s == null || s.isEmpty() || s.trim() == "")
			return true;
		else
			return false;
	}
	public static String toEmptyStringIfNull(Object o) {
		if (o == null)
			return "";
		else
			return o.toString();
	}
	 public static String join(char c,int[] ss){
	    	String ret="";
	    	if(ss!=null){
	    		for(int s:ss){
	    			ret+=s+""+c;
	    		}
	    		return trimEnd(',', ret);
	    	}
	    	return null;
	    }
    public static String join(char c,String[] ss){
    	String ret="";
    	if(ss!=null){
    		for(String s:ss){
    			ret+=s+c;
    		}
    		return trimEnd(',', ret);
    	}
    	return null;
    }
    public static String trim(char c, String s)
    {
    	return  trim(new char[]{c},s);
    }
    public static String trim(char[] trimChars, String s)
    {
        if ((trimChars != null) && (trimChars.length != 0))
        {
            return trimHelper(s,trimChars,2);
        }
        return s;
    }
    public static String trimEnd(char c, String s) {
		
		return  trimEnd(new char[]{c},s);

	}
	public static String trimEnd(char[] trimChars, String s) {
		if ((trimChars != null) && (trimChars.length != 0)) {
			return trimHelper(s, trimChars, 1);
		}
		return s;

	}

	private static String trimHelper(String s, char[] trimChars, int trimType) {
		int end = s.length() - 1;
		int start = 0;
		if (trimType != 1) {
			start = 0;
			while (start < s.length()) {
				int index = 0;
				char ch = s.charAt(start); // this[start];
				index = 0;
				while (index < trimChars.length) {
					if (trimChars[index] == ch) {
						break;
					}
					index++;
				}
				if (index == trimChars.length) {
					break;
				}
				start++;
			}
		}
		if (trimType != 0) {
			end = s.length() - 1;
			while (end >= start) {
				int num4 = 0;
				char ch2 = s.charAt(end);
				num4 = 0;
				while (num4 < trimChars.length) {
					if (trimChars[num4] == ch2) {
						break;
					}
					num4++;
				}
				if (num4 == trimChars.length) {
					break;
				}
				end--;
			}
		}
		return createTrimmedString(s, start, end);
	}

	private static String createTrimmedString(String s, int start, int end) {
		int length = (end - start) + 1;
		if (length == s.length()) {
			return s;
		}
		if (length == 0) {
			return "";
		}
		return s.substring(start, start + length);
	}
}
